package com.security.demo;

import org.springframework.security.web.DefaultRedirectStrategy;
import org.springframework.security.web.RedirectStrategy;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.util.Date;

@RestController
public class Demo {

    private final RedirectStrategy redirectStrategy = new DefaultRedirectStrategy();

    @RequestMapping("/redirect")
    public String hello(HttpServletRequest request, HttpServletResponse response) throws IOException {
        redirectStrategy.sendRedirect(request, response, "http://www.baidu.com");
        return "redirect";
    }


    @RequestMapping("/hello")
    public String hello() {
        return "hello";
    }


    @RequestMapping("/file")
    public String hello(MultipartFile file) throws IOException {
        System.out.println(file.getName());
        System.out.println(file.getOriginalFilename());
        System.out.println(file.getSize());
        File localFile = new File("C:\\java\\demo\\spring-security-demo\\src\\main\\resources", new Date().getTime() + ".txt");
        file.transferTo(localFile);
        return "hello";
    }

}
